<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/31
 * Time: 15:12
 */
namespace Controller;
use Model\IndexModel;
use Shop\Controller;

if(!defined('SHOP')){
    header("Location:/index.php");
}

class LoginController extends Controller{

    //登录方法
    public function login(){
        if($_POST){
            $email=$_POST['email'];
            $pass=$_POST['pass'];
            $model=new IndexModel();
            $member=$model->pdo_sel("select * from sh_member where email = '$email'");
            if(empty($member)){
                $this->success("邮箱名不存在");
            }

            if($member['pass']!=md5($pass)){
                $this->success("密码不正确");
            }
            //保存数据到session
            $_SESSION['Home']['Member']=$member;
            $this->success("登录成功","Member","index");
        }
        $this->view->display();
    }

    //显示验证码方法
    public function idenYzm(){
        $this->yzm();
    }
    //注册方法
    public function register(){
        if($_POST){
            //判断
            $email=$_POST['email'];
            $emyzm=$_POST['emyzm'];
            $pass1=$_POST['pass1'];
            $pass2=$_POST['pass2'];

            if($email!=$_SESSION['Home']['reg']['email']||$emyzm!=$_SESSION['Home']['reg']['code']){
                $this->success("验证码或者邮箱错误！");
            }

            if($pass1!=$pass2){
                $this->success("两次密码不一致！");
            }

            //插入数据库
            $model=new IndexModel();
            $arr=array();
            $arr['email']=$email;
            $arr['pass']=md5($pass1);
            $arr['time']=time();
            if($model->pdo_insert('sh_member',$arr)){
                $this->success("注册成功！",'Login','login');
            }else{
                $this->success("注册失败！");
            }


        }
        $this->view->display();
    }

    //验证邮箱和验证码
    public function yzR(){
        $email=$_POST['email']; //邮箱地址
        $yzm=$_POST['yzm']; // 验证码

        //定义返回结果
        if($yzm!=$_SESSION['Home']['yzm']){
            $arr=array('type'=>1,'msg'=>'验证码错误');
            $this->retuAjax($arr);
        }
        //邮箱是否存在是否合法(这个是正则匹配主要用来判断数据格式)
        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
        if (!preg_match( $pattern, $email))
        {
            $arr=array('type'=>1,'msg'=>'邮箱格式不正确');
            $this->retuAjax($arr);
        }
        //判断是否存在
        $model=new IndexModel();
        $cou=$model->pdo_sel("select count(*) as co from sh_member where email = '$email'");
        if($cou['co']>0){
            $arr=array('type'=>1,'msg'=>'邮箱已经存在');
            $this->retuAjax($arr);
        }

        //邮箱合法---发送邮件
        $code=rand(1000,9999);
        $_SESSION['Home']['reg']['code']=$code;
        $_SESSION['Home']['reg']['email']=$email;

        if($this->sendMailer($email,$code,'注册验证码')){
            $arr=array('type'=>2,'msg'=>'发送成功');
            $this->retuAjax($arr);
        }else{
            $arr=array('type'=>1,'msg'=>'发送失败');
            $this->retuAjax($arr);
        }

    }

    //退出方法
    public function tc(){
        unset($_SESSION['Home']);

        $this->redirect('Login','login');
    }

}